/**
 * Created by wenwu on 02/05/2017.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');

    var id = utilities.getParameterByName('id');

	var app = new Vue({
		el: '#enroll_sign',
		data: {
			formObj: {
                nickname: '',
				phone: '',
                code:'',
				sex: '',
                birthday: '',
                height: '',
                weight: '',
                nation: '请选择',
                nativePlace: '请选择',
                profession: '请选择',
                education: '请选择',
                marry: '请选择',
                houseStatus: '请选择',
                carStatus: '请选择',
                area: '请选择',
                mateAge: '',
                mateIncome: '',
                mateArea: '',
                mateHeight: '',
                mateNative: '不限',
                mateEducation: '',
                photo:'',
                mateMarry: '',
                introduction: ''
			},
            selections: {
                424: []
            },
            marriageList: [],
            educationList: [],
            nativeList: [],
            cityLists: [],
			rePassword: '',
			cityList: [],
			regionList: [],
            smsSeconds: 60,
            smsAvailable: true
		},
		methods: {
            loadArea: function (arrName, code, callback) {
                _(Config.SERVICE.COMMON.ADDRLIST, {
                    parentId: code
                }, function (data) {
                    app[arrName] = data.obj[code];
                    app[arrName].unshift({id: '', name: '请选择'});
                    if (typeof callback === 'function')
                        callback();
                });
            },
			init: function () {
                _(Config.DICTIONARY, {
                    parentId: '418,424,437,450,8,6,7,457,463,467,1010,975,340,975'
                }, function (data) {
                    for (var key in data.obj) {
                        data.obj[key].unshift({id: '', name: '请选择'});
                    }
                    app.selections = data.obj;

                    _(Config.DICTIONARY, {
                            parentId: '975'
                    }, function (data) {
                        data.obj[975].unshift({id: '', name: '不限'});
                        app.nativeList=data.obj[975];
                    });
                    var marriageList = app.removeUnused(data.obj[6]);
                    var educationList = app.removeUnused(data.obj[8]);

                    app.restoreCheckList(marriageList, app.formObj.mateMarry.split(','));
                    app.restoreCheckList(educationList, app.formObj.mateEducation.split(','));

                    app.marriageList = marriageList;
                    app.educationList = educationList;

                    _(Config.SERVICE.COMMON.ADDRLIST, {
                        parentId: 2
                    }, function (data) {
                        var cityList = data.obj[2];
                        app.restoreCheckList(cityList, app.formObj.mateArea.split(','));
                        app.cityLists = cityList;
                    });

                    app.loadArea('cityList', 2);
                });
			},
            restoreCheckList: function (fullListArr, checkedArr) {
                for (var i = 0; i < fullListArr.length; i++) {
                    for (var j = 0; j < checkedArr.length; j++) {
                        if (fullListArr[i].name == checkedArr[j]) {
                            fullListArr[i].checked = true;
                        }
                    }
                }
            },
            getName: function (arr, id) {
                for (var i = 0; i < arr.length; i++)
                    if (arr[i]['id'] == id)
                        return arr[i]['name'];
            },
            removeUnused: function (arr) {
                var _arr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].name != '不透露') {
                        _arr.push(arr[i]);
                    }
                }
                return _arr;
            },
            pick: function (listName, i) {
                app.$set(listName + '[' + i + '].checked', !app[listName][i].checked);
            },
            isChecked: function (item) {
                return !!item.checked;
            },
            getCheckedStr: function (arr) {
                var str = '';
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].checked && arr[i].id!='')
                        str += (arr[i].name + ',');
                }
                return str.substr(0, str.length - 1);
            },
			submit: function () {
                for (var key in app.formObj) {
                    if(key!='mateMarry'&&key!='mateEducation'&&key!='mateArea'){
                        if (app.formObj[key].trim() === ''){
                            alert('请填写完整！');
                            return;
                        }
                    }
                }
				if (app.formObj.nickname.trim() === '') {
					alert('请输入姓名！');
				}else if (app.formObj.sex.trim() === '') {
                    alert('请选择性别！');
                }else if (app.formObj.phone.trim() === '') {
					alert('请输入手机号！');
				}else if (app.formObj.code.trim() === '') {
                    alert('请输入验证码！');
                }else {
					var req = {};
					for (var key in app.formObj) {
						if(key!='code')
							req['enroll.' + key] = app.formObj[key];
						else{
                            req['code']=app.formObj[key];
						}
					}

					req['enroll.blind.id'] = id;
                    req['enroll.mateMarry'] = app.getCheckedStr(app.marriageList);
                    req['enroll.mateEducation'] = app.getCheckedStr(app.educationList);
                    req['enroll.mateArea'] = app.getCheckedStr(app.cityLists);

					_(Config.SERVICE.ACTIVITY.ENROLLSIGN, req, function (data) {
						if (data.type === 'SUCCESS') {
							alert('报名成功！');
							sessionStorage.setItem('qtid', data.obj.id);
							location.href = 'enroll_activity.html?typeId='+id;
						} else {
							alert(data.errmsg);
						}
					});
				}
			},
			goPhoneBind: function(){
				location.href = 'enroll_login.html?id=' + id;
			},
            sendCaptcha: function () {
                if(app.smsAvailable){
                    app.smsSeconds = 60;
                    if (app.formObj.phone.trim() === '') {
                        alert('请输入手机号！');
                    } else {
                        _(Config.SERVICE.USERCENTER.SEND_CAPTCHA, {phone: app.formObj.phone}, function (data) {
                            console.log(data);
                            app.smsAvailable = false;
                            var timer = setInterval(function () {
                                if (app.smsSeconds == 1) {
                                    app.smsAvailable = true;
                                    clearInterval(timer);
                                }
                                app.smsSeconds--;
                            }, 1000);
                        });
                    }
                }
            }
		}
	});
	app.init();
    //头像上传
    $('#figure').AjaxFileUpload({
        action: Config.SERVICE.USERCENTER.AVATARUPLOAD,
        onComplete: function (filename, response) {
            //console.log(response.url);
            app.formObj.photo='http://www.qqlian.org/'+response.url;
        }
    });
});